package blackeagle.ems.entities;

import blackeagle.ems.BlackEagleEMS;
import blackeagle.ems.dal.DataClass;
import blackeagle.ems.dal.DataField;
import blackeagle.ems.dal.DataRecord;
import blackeagle.ems.dal.MethodType;
import java.util.ArrayList;
import java.util.HashMap;
import se.oru.informatik.InformatikDB;
import se.oru.informatik.InformatikException;
import se.oru.informatik.StringHelper;

/**
 * A class representing a subproject
 */
@DataClass(dataName="SUBPROJECT")
public class Subproject extends DataRecord<Subproject> {
    private Integer id, leadId, projectId, domainId;
    private String name, description;

    /**
     * The default 0-parameter constructor
     */
    public Subproject(){}
    
    /**
     * A static class containing readonly strings with column names.
     */
    public static class Columns{
        public static final String Id = "id";
        public static final String LeadId = "leadid";
        public static final String DomainId = "domainid";
        public static final String ProjectId = "projectid";
        public static final String Name = "name";
        public static final String Description = "description";
        
        private Columns(){}
    }
    
    @DataField(fieldName="description", methodType=MethodType.Getter)
    public String getDescription() {
        return description;
    }

    @DataField(fieldName="description", methodType=MethodType.Setter)
    public void setDescription(String description) {
        this.description = description;
        this._isDirty=true;
    }
    
    public Domain getDomain(){
        Domain d = new Domain().load(this.domainId);
        return (d.isNew()?null:d);
    }
    
    public void setDomain(Domain domain){
        Integer i = domain.getId();
        this.setDomainId(i);
    }

    @DataField(fieldName="domainid", methodType=MethodType.Getter)
    public Integer getDomainId() {
        return domainId;
    }

    @DataField(fieldName="domainid", methodType=MethodType.Setter)
    public void setDomainId(Integer domainId) {
        this.domainId = domainId;
        this._isDirty=true;
    }

    @DataField(fieldName="id", methodType=MethodType.Getter, isPrimary=true, autoIncrement=true)
    public Integer getId(){
        return this.id;
    }
    
    @DataField(fieldName="id", methodType=MethodType.Setter, isPrimary=true, autoIncrement=true)
    public void setId(Integer value){
        if(value>0){
            this.id=value;
            this._isDirty=true;
        }        
    }
    
    public Employee getLead(){
        Employee e = new Employee().load(this.leadId);
        return (e.isNew()?null:e);
    }
    
    public void setLead(Employee lead){
        Integer i = lead.getId();
        this.setLeadId(i);
    }

    @DataField(fieldName="leadid", methodType=MethodType.Getter)
    public Integer getLeadId() {
        return leadId;
    }

    @DataField(fieldName="leadid", methodType=MethodType.Setter)
    public void setLeadId(Integer leadId) {
        this.leadId = leadId;
        this._isDirty=true;
    }

    @DataField(fieldName="name", methodType=MethodType.Getter)
    public String getName() {
        return name;
    }

    @DataField(fieldName="name", methodType=MethodType.Setter)
    public void setName(String name) {
        this.name = name;
        this._isDirty=true;
    }
    
    public Project getProject(){
        Project p = new Project().load(this.projectId);
        return (p.isNew()?null:p);
    }
    
    public void setProject(Project project){
        Integer i = project.getId();
        this.setProjectId(i);
    }

    @DataField(fieldName="projectid", methodType=MethodType.Getter)
    public Integer getProjectId() {
        return projectId;
    }

    @DataField(fieldName="projectid", methodType=MethodType.Setter)
    public void setProjectId(Integer projectId) {
        this.projectId = projectId;
        this._isDirty=true;
    }
    
    /**
     * Attempts to fetch all the employees assigned to this subproject as specialists.
     * @return the employees.
     */
    public EmployeeCollection getAssignedEmployees(){
        EmployeeCollection ec = new EmployeeCollection();
        InformatikDB iDB = null;
        try{
            iDB=new InformatikDB(BlackEagleEMS.connectionString);
        }catch(InformatikException e){
            return ec;
        }
        
        String query = StringHelper.sprintf(
                    "SELECT E.* FROM EMPLOYEE AS E INNER JOIN SUBPROJECTSPECIALIST AS SPS ON(SPS.EMPLOYEEID=E.ID) WHERE SPS.SUBPROJECTID='#'",
                    this.id);
        ArrayList<HashMap<String,String>> result = null;
        try{
            result=iDB.fetchRows(query);
                    
        }catch(InformatikException e){
            
        }
        
        ec.loadData(result);
        
        return ec;
    }
    
    @Override
    public String toString(){
        return this.name;
    }
}
